﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class translate_service : base_service
    {
        //get all get all
        public PagedList<translate> translate_plist(int pageindex, int pagesize, int language_id, int groupid)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_translates
                            where dt.language_id == language_id && dt.trans_group == groupid
                            select dt);
                list = list.OrderBy(p => p.trans_id);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        //get list translate, to create xml file
        public List<v_translate> get_list_trans(int groupid, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from lg in data.view_translate
                            where lg.trans_group == groupid && lg.language_id == language_id
                            orderby lg.trans_id ascending
                            select lg);
                return list.ToList();
            }
        }

        public long trans_update(translate it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_translates.Where(p => p.trans_label == it.trans_label
                                                && p.language_id == it.language_id
                                                && p.trans_group == it.trans_group).FirstOrDefault();
                if (item != null)
                { 
                    item.language_id = it.language_id;
                    item.trans_label = it.trans_label;
                    item.trans_text = it.trans_text;


                    data.SaveChanges();
                    return item.trans_id;
                }
                else
                {
                    data.AddTotbl_translates(it);
                    data.SaveChanges();
                    return it.trans_id;
                }
            }
        }

        //get items by id or code
        public translate getByID(string lbl, int groupid, int langid)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_translates.Where(p => p.trans_label == lbl
                                                && p.language_id == langid
                                                && p.trans_group == groupid).FirstOrDefault();
                if (item == null)
                    item = new translate();
                return item;
            }
        }

        public List<translate> ListByLbl(string lbl, int groupid)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_translates.Where(p => p.trans_label == lbl
                                                && p.trans_group == groupid);
                // if (item == null)
                //    item = new translate();
                return item.ToList<translate>();
            }
        }
    }
}
